﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace VirtualTournaments.Frontend.MenuControls.StatisticsMenu
{
    using TournamentsService = Services.Tournaments.TournamentsService;
    using Tournaments = Services.Tournaments.Tournament;
    using Team = Services.Tournaments.Team;
    using TeamsService = Services.Tournaments.TeamsService;
    using PlayersService = Services.Tournaments.PlayersService;
    using Player = Services.Tournaments.Player;

    public partial class TournamentSubscriptionMenu : System.Web.UI.UserControl
    {
        public EventHandler GoBackButtonClickedBridge;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.MainMenu.GoBackButtonClicked += new EventHandler(TournamentSubscriptionMenu_GoBackButtonClicked);
            this.MainMenu.GoToTournamentInfo += new EventHandler(TournamentSubscriptionMenu_GoToTournamentInfoButtonClicked);
            this.TournamentInfo.GoToTeamInfo += new EventHandler(TournamentSubscriptionMenu_GoToTeamInfoButtonClicked);
        }

        protected void TournamentSubscriptionMenu_GoBackButtonClicked(object sender, EventArgs e)
        {
            //TODO: find a better way to do this
            VirtualTournamentsFrontendEvent realEvent = e as VirtualTournamentsFrontendEvent;

            if (realEvent != null)
                if (realEvent.SenderMenu.Equals("TournamentSubscriptionMainMenu"))
                    GoBackButtonClickedBridge(this, new VirtualTournamentsFrontendEvent { SenderMenu = "TournamentSubscriptionMenu" });
        }

        protected void TournamentSubscriptionMenu_GoToTournamentInfoButtonClicked(object sender, EventArgs e)
        {
            //TODO: find a better way to do this
            VirtualTournamentsFrontendEvent realEvent = e as VirtualTournamentsFrontendEvent;

            if (realEvent != null)
                TournamentInfo.TournamentName = realEvent.TournamentName;

            SetTeamsInTournament(TournamentInfo.TournamentName);

            //TODO: constants
            MenuViews.ActiveViewIndex = 1;
        }

        protected void TournamentSubscriptionMenu_GoToTeamInfoButtonClicked(object sender, EventArgs e)
        {
            //TODO: find a better way to do this
            VirtualTournamentsFrontendEvent realEvent = e as VirtualTournamentsFrontendEvent;

            if (realEvent != null)
                TeamInfo.TeamName = realEvent.TeamName;

            //TODO: constants
            MenuViews.ActiveViewIndex = 2;
        }

        protected List<string> GetTournamentTeamList(string TournamentName) 
        {
            List<string> teams = new List<string>();
            string team = string.Empty;
            for (int i = 0; i < 20; i++)
            {
                team = "Team " + i;
                teams.Add(team);
            }

            return teams;
        }

        protected void SetTeamsInTournament(string TournamentName) 
        {
            List<string> teams = GetTournamentTeamList(TournamentName);

            foreach (string team in teams)
                TournamentInfo.TeamsListBox.Items.Add(new ListItem(team));
        }

    }
}